/************************************************************
  *  * EaseMob CONFIDENTIAL 
  * __________________ 
  * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. 
  *  
  * NOTICE: All information contained herein is, and remains 
  * the property of EaseMob Technologies.
  * Dissemination of this information or reproduction of this material 
  * is strictly forbidden unless prior written permission is obtained
  * from EaseMob Technologies.
  */

#import "DXFaceView.h"

@interface DXFaceView ()<UIScrollViewDelegate>
{
    FacialView *_facialView;
}

@property (strong,nonatomic) UIScrollView *scrollView;

@property (strong,nonatomic) UIPageControl *pageControl;

@property (strong,nonatomic) UIButton *sendButton;

@end

@implementation DXFaceView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //        _facialView = [[FacialView alloc] initWithFrame: CGRectMake(5, 5, frame.size.width - 10, self.bounds.size.height - 10)];
        //
        //        FacialView *fview=[[FacialView alloc] initWithFrame:CGRectMake(12+SCREEN_W*i, 15, SCREEN_W-24, self.bounds.size.height-45)];
        //        [fview setBackgroundColor:[UIColor clearColor]];
        //        [_facialView loadFacialView:1 size:CGSizeMake(30, 30)];
        //        _facialView.delegate = self;
        //        [self addSubview:_facialView];
        [self makeView];
    }
    return self;
}
-(void)makeView
{
    
    NSLog(@"%f---%f",self.bounds.size.width,self.bounds.size.height);
    _scrollView=[[UIScrollView alloc] initWithFrame:self.bounds];
    
    _scrollView.backgroundColor = RGBACOLOR(245, 245, 245, 1);
    
    
    for (int i=0; i<2; i++) {
        FacialView *fview=[[FacialView alloc] initWithFrame:CGRectMake(12+ScreenWidth*i, 15, ScreenWidth-24, self.bounds.size.height-45)];
        [fview setBackgroundColor:[UIColor clearColor]];
        [fview loadFacialView:i size:CGSizeMake((ScreenWidth-24)/8, (self.bounds.size.height-45)/3)];
        fview.delegate=self;
        
        [_scrollView addSubview:fview];
    }
    
    [_scrollView setShowsVerticalScrollIndicator:NO];
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    _scrollView.contentSize=CGSizeMake(ScreenWidth*2, self.bounds.size.height);
    _scrollView.pagingEnabled=YES;
    _scrollView.delegate=self;
    
    [self addSubview:_scrollView];
    
    _pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(12, self.bounds.size.height-30, ScreenWidth-62-12, 30)];
    
    [_pageControl setCurrentPage:0];
    
    _pageControl.pageIndicatorTintColor = RGBACOLOR(195, 179, 163, 1);
    
    _pageControl.currentPageIndicatorTintColor = RGBACOLOR(132, 104, 77, 1);
    
    _pageControl.numberOfPages = 2;//指定页面个数
    
    [_pageControl setBackgroundColor:[UIColor clearColor]];
    
    [_pageControl addTarget:self action:@selector(changePage:)forControlEvents:UIControlEventValueChanged];
    
    [self addSubview:_pageControl];
    
    _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [ _sendButton setTitle:@"发送" forState:UIControlStateNormal];
    
    [_sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    _sendButton.backgroundColor = self.tintColor;
    
    _sendButton.frame = CGRectMake(ScreenWidth-50-12, self.bounds.size.height-30, 50+12, 30);
    
    [_sendButton addTarget:self action:@selector(pressSendBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_sendButton];
    
    
    
}

#pragma mark - FacialViewDelegate

-(void)selectedFacialView:(NSString*)str{
    if (_delegate) {
        [_delegate selectedFacialView:str isDelete:NO];
    }
}

-(void)deleteSelected:(NSString *)str{
    if (_delegate) {
        [_delegate selectedFacialView:str isDelete:YES];
    }
}

- (void)pressSendBtn
{
    if (_delegate) {
        [_delegate sendFace];
    }
}

-(void)changePage:(UIPageControl *)pageControl
{
    NSInteger page = _pageControl.currentPage;//获取当前pagecontroll的值
    [_scrollView setContentOffset:CGPointMake(ScreenWidth * page, 0)];//根据pagecontroll的值来改变scrollview的滚动位置，以此切换到指定的页面
    
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    int page = _scrollView.contentOffset.x / ScreenWidth;//通过滚动的偏移量来判断目前页面所对应的小白点
    _pageControl.currentPage = page;//pagecontroll响应值的变化
}

#pragma mark - public

- (BOOL)stringIsFace:(NSString *)string
{
    if ([_facialView.faces containsObject:string]) {
        return YES;
    }
    
    return NO;
}

@end
